#include "log.h"
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

CDebugLog *CDebugLog::pDebugLog = NULL;

CDebugLog::CDebugLog(DEBUGLEVEL level)
{
    pthread_mutex_init(&m_Mutex, NULL);
    filename = NULL;
    debugLevel = level;
    fp = NULL;
    SetLogFileName("camera.log");
}

int CDebugLog::SetLogFileName(const char *logFileName)
{
    if(NULL == logFileName)
        return -1;
    if(NULL != filename)
    {
        free(filename);
        filename = NULL;
    }
    char *tmpBuf = get_current_dir_name();
    filename = (char *)malloc(strlen(logFileName) + strlen(tmpBuf) + 1);
    memset(filename, 0, strlen(logFileName) + strlen(tmpBuf) + 1);
    memcpy(filename, tmpBuf, strlen(tmpBuf));
    memcpy(filename, "/", 1);
    memcpy(filename + strlen(tmpBuf) + 1, logFileName, strlen(logFileName));

    if(access(filename, F_OK) < 0)
    {
        if(mkdir(filename, 0777 ) < 0 )
            return -1;
    }
    memset(filename, 0, strlen(logFileName) + 1);
    memcpy(filename, logFileName, strlen(logFileName));

    if(NULL != fp)
    {
        fclose(fp);
        fp = NULL;
    }
    fp = fopen(filename, "a+");
    if(fp && fseek(fp, 0, SEEK_END)>-1)
    {
        if(ftell(fp) >= 100*1024*1024)   //文件大于100M时删除此文件
        {
            fclose(fp);
            fp = NULL;
            fp = fopen(filename, "w+");
            DebugLog(debugLevel, "The log file is too big, clear it!");
        }
        DebugLog(debugLevel, "The Player version is 2014-04-02");
    }
    return 0;
}

CDebugLog *CDebugLog::Instance()
{
    if (NULL == pDebugLog)
            pDebugLog = new CDebugLog(EDEBUG_LEVEL_MESSAGE);
    return pDebugLog;
}

CDebugLog::~CDebugLog()
{
    if(0 == pthread_mutex_trylock(&m_Mutex))
    {
        pthread_mutex_lock(&m_Mutex);
        if(NULL != fp)
        {
            fclose(fp);
            fp = NULL;
        }
        if(NULL != filename)
        {
            free(filename);
            filename = NULL;
        }
        pthread_mutex_unlock(&m_Mutex);
        pthread_mutex_destroy(&m_Mutex);
    }
}

void CDebugLog::DebugLog(DEBUGLEVEL level, const char *arg, ...)
{
    pthread_mutex_lock(&m_Mutex);
    memset(time_buf, 0, LOG_STR_MAX_LEN);
    memset(str_buf, 0, LOG_STR_MAX_LEN);

    time_t tm;
    time(&tm);
    struct tm *timeinfo = localtime(&tm);
    strcpy(str_buf, asctime(timeinfo));
    strcpy(str_buf + strlen(str_buf) - 1, " : ");

    va_list list;
    va_start(list, arg);
    vsprintf(time_buf, arg, list);
    va_end(list);

    strcpy(str_buf + strlen(str_buf) - 1, time_buf);

    if(fp && debugLevel>=level)  //写入日志文件
    {
        printf("%s\n", str_buf);
        fflush(stdout);
        strcat(str_buf, "\n");
        fwrite(str_buf, 1, strlen(str_buf), fp);
        fflush(fp);
    }
    pthread_mutex_unlock(&m_Mutex);
}

void CDebugLog::DebugLog(const char *arg, ...)
{
    pthread_mutex_lock(&m_Mutex);
    memset(time_buf, 0, LOG_STR_MAX_LEN);
    memset(str_buf, 0, LOG_STR_MAX_LEN);

    time_t tm;
    time(&tm);
    struct tm *timeinfo = localtime(&tm);
    strcpy(str_buf, asctime(timeinfo));
    strcpy(str_buf + strlen(str_buf) - 1, " : ");

    va_list list;
    va_start(list, arg);
    vsprintf(time_buf, arg, list);
    va_end(list);

    strcpy(str_buf + strlen(str_buf) - 1, time_buf);

    if(fp)  //写入日志文件
    {
        printf("%s\n", str_buf);
        fflush(stdout);
        strcat(str_buf, "\n");
        fwrite(str_buf, 1, strlen(str_buf), fp);
        fflush(fp);
    }
    pthread_mutex_unlock(&m_Mutex);
}











































